package com.lanjian.idbank.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 调用阿里云接口对用户银行卡信息进行真实性认证
 */
public class RealBankCardAuthentication {

    public static void main(String[] args) {

        //"state":1

        Integer s = RealBankCardAuthentication.VerifyBankCard("兰剑", "18747821696", "152825199605236013", "1231241241241");
        System.out.println(s);
    }


    public static final String host="https://dfbankv2.market.alicloudapi.com";
    public static final String path="/verify_id_name_bankcard_phone_v2";
    public static final String method="POST";
    public static final String appcode="b3eacc441986486e8cd0f445e9d5f642"; //填写自己AppCode

    /**
     *  银行卡判断4要素
     * @param name        用户姓名
     * @param phone       用户手机号
     * @param idNumber    用户身份证号
     * @param cardNumber  用户银行卡号
     */
    public static Integer VerifyBankCard(String name,String phone,String idNumber,String cardNumber) {
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("card_number", cardNumber);
        bodys.put("id_number", idNumber);
        bodys.put("name", name);
        bodys.put("phone_number", phone);

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //该方法只能调用一次，调用一次后，流会关闭
            String entityString = EntityUtils.toString(response.getEntity());
            System.out.println(entityString);
            //因为是 entityString 是 String数据类型，所以我们需要 转换成为 JSON 字符串，通过JSON字符串获取到 JSON 对应的respCode 编码，来获取所对应的信息
            JSONObject jsonObject = JSONArray.parseObject(entityString);
            //获取state  1 审核通过    2、审核不通过    若state == null  身份证、手机号、用户真实姓名有误！
            Integer state = (Integer) jsonObject.get("state");
            return state;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return  0;
    }

}
